package com.example.arithmeticleetcode.leetcode.nov;

import java.util.*;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-12-03 13:08
 **/
public class Demo25 {



    public static int maximum69Number (int num) {
        String s = String.valueOf(num);
        StringBuilder sb = new StringBuilder();
        boolean flag = true;
        for (int i = 0, len = s.length() - 1; i <= len; i++) {
            if (flag && s.charAt(i) == 54) {
                sb.append(9);
                flag = false;
                continue;
            }
            sb.append(s.charAt(i));
        }
        return Integer.valueOf(sb.toString());

    }

    public static List<String> printVertically(String s) {
        String[] strings = s.split(" ");
        //求出最大的长度与最小的长度
        int maxLen = strings[0].length();
        for (int i = 1, len = strings.length; i < len; i++) {
            int tempLen = strings[i].length();
            if (maxLen <= tempLen) {
                maxLen = tempLen;
            }
        }
        Map<Integer, StringBuilder> map = new HashMap<>();
        for (int j = 0, sLen = strings.length; j < sLen; j++) {
            String tempStr = strings[j];
            for (int i = 0; i < maxLen; i++) {
                char c = i < tempStr.length() ? tempStr.charAt(i) : ' ';
                if (map.containsKey(i)) {
                    StringBuilder stringBuilder = map.get(i);
                    stringBuilder.append(c);
                } else {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(c);
                    map.put(i, stringBuilder);
                }
            }
        }
        String[] result = new String[maxLen];
        for (int i = 0, len = result.length; i < len; i++) {
            result[i] = toLastTrim(map.get(i));
        }
        return Arrays.asList(result);
    }

    public static void main(String[] args) {
        String str = "HOW ARE YOU";
        String str1 = "TO BE OR NOT TO BE";
        String str2 = "CONTEST IS COMING";
        System.out.println(printVertically(str));
        System.out.println(printVertically(str1));
        System.out.println(printVertically(str2));

        System.out.println("----------------");

        String str3 = "T  ";
        System.out.println(toLastTrim(new StringBuilder(str3)) + "000");
    }

    private static String toLastTrim(StringBuilder sb) {
        String str = sb.toString();
        int star = 0;
        for (int i = str.length() - 1; i >= 0; i--) {
            char c = str.charAt(i);
            if (c != ' ') {
                star = i;
                break;
            }
        }
        return str.substring(0, star + 1);
    }
}
